import jsonBig from 'json-bigint'
import {
	tools
} from "./util";

let showShowModal = false;
const myRequest = (uri, data, method = 'GET', options = {}, url) => {
	if (!url) {
		url = "https://ai.yainoo.com/api/";
		if (tools.vadIp == '121.196.245.10') {
			url = "https://ai.lsjz-ai.com/api/";
		}
		// // #ifdef H5
		// url = "/api"
		// // #endif
	}
	method = method.toLocaleUpperCase();
	const token = uni.getStorageSync('token') || uni.getStorageSync('_token');
	return new Promise((resolve, reject) => {
		let pages = [];
		try {
			pages = getCurrentPages();
			let page = pages[pages.length - 1];
			if (page?.route == 'pages/createAvatar/createAvatar') {
				uni.showLoading({
					title: '请稍等...'
				});
			}
		} catch (e) {
			//TODO handle the exception
		}
		let opt = {
			url: (url + uri).replace(url + '/', url),
			method: method,
			dataType: 'String',
			data,
			timeout: 1000 * 60 * 8,
			header: {
				'Authorization': token,
				'Accept': method == 'POST' ? 'application/json, text/plain, */*' : '*/*',
			},
			success(res) {
				try {
					res.data = (jsonBig.parse(res.data));
				} catch (err) {
					res.data = res.data;
				}
				// 接口状态码
				if (typeof res.data == 'object') {
					const code = res.data.code;
					if (code == 10200 || code == 1) {
						resolve(res.data);
					} else {
						if (code == 10005) {
							resolve(false);
							uni.removeStorage({
								key: 'token',
							});
							uni.removeStorage({
								key: 'userInfo',
							});
							if (!showShowModal) {
								showShowModal = true;
								uni.showModal({
									title: '提示',
									content: '暂未登录，请先登录！',
									success: function(res) {
										showShowModal = false;
										if (res.confirm) {
											let pages = getCurrentPages();
											let page = pages[pages.length - 1];
											if (page.$vm.$refs.LoginDialog) {
												page.$vm.LoginDialogShow = true;
											} else {
												uni.navigateTo({
													url: '/pages/login/login'
												});
											}
										} else if (res.cancel) {
											console.log('用户点击取消');
										};
									}
								});

							};
							return;
						};

						uni.showModal({
							title: '提示',
							content: res.data.message || '亲,服务器内部错误,请联系管理员处理...',
							success: function(res) {
								if (res.confirm) {} else if (res.cancel) {};
							}
						});
						resolve(false);
					};
				} else if (typeof res.data == 'string') {
					console.log('string', res);
					resolve(false);
				} else {
					// 提示报错
					console.log('提示报错', res, (url + uri).replace('https://ai.yainoo.com/api//',
						'https://ai.yainoo.com/api/'), );
					resolve(false);
				}

			},
			fail(err) {
				if (err.errMsg == "request:fail timeout") {
					tools.easyToast('请求超时!');
				}
				reject(err);
			},
			complete(val) {
				uni.hideLoading();
			},
			...options
		};
		uni.request(opt);
	});
};

export function post(uri, data, opt, url) {
	return myRequest(uri, data, 'post', opt, url);
};
export function get(uri, data, opt, url) {
	return myRequest(uri, data, 'get', opt, url);
};
export default myRequest;